package sample.hybrid;

import android.Manifest;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.lambda.hybrid.Callback;
import com.lambda.hybrid.utils.Util;

public class MainActivity extends AppCompatActivity {
    private HybridBrowser mBrowser;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActivityCompat.requestPermissions(this, new String[]{
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.ACCESS_WIFI_STATE,
        }, 1000);

        mBrowser = findViewById(R.id.browser);

        View statusBar = findViewById(R.id.status_bar);
        ViewGroup.LayoutParams params = statusBar.getLayoutParams();
        params.height = Util.getStatusBarHeight(getResources());
        statusBar.setLayoutParams(params);
        mBrowser.setStatusBar(statusBar);
        mBrowser.setToolbar(findViewById(R.id.toolbar));

        findViewById(R.id.call).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBrowser.invoke("addItem", "time: " + System.currentTimeMillis(), new Callback() {
                    @Override
                    public void invoke(String data) {
                        Toast.makeText(MainActivity.this, data, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

        mBrowser.loadUrl("file:////android_asset/index.html");
    }

    @Override
    protected void onDestroy() {
        mBrowser.destroy();
        super.onDestroy();
    }
}
